package com.example.runable;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.entity.User;
import com.example.entity.User2;
import com.example.service.UserService2;
import com.example.vo.IdxInfoVO;
import com.example.vo.TableDDLVO;
import com.example.vo.TableInfoVO;
import com.example.vo.TableVO;
import com.example.helper.AsnyHelper;
import com.example.mapper.CommonMapper;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@Slf4j
public class OracleDemoRunner implements ApplicationRunner {

    @Autowired
    UserService userService;

    @Autowired
    UserService2 userService2;

    @Autowired
    AsnyHelper asnyHelper;

    @Autowired
    CommonMapper commonMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        method06();
    }

    private void method05() {
        String c1="package com.xiets.javamaildemo;\n" +
                "\n" +
                "import java.util.Date;\n" +
                "import java.util.Properties;\n" +
                "\n" +
                "import javax.activation.DataHandler;\n" +
                "import javax.activation.FileDataSource;\n" +
                "import javax.mail.Message.RecipientType;\n" +
                "import javax.mail.Session;\n" +
                "import javax.mail.Transport;\n" +
                "import javax.mail.internet.InternetAddress;\n" +
                "import javax.mail.internet.MimeBodyPart;\n" +
                "import javax.mail.internet.MimeMessage;\n" +
                "import javax.mail.internet.MimeMultipart;\n" +
                "import javax.mail.internet.MimeUtility;\n" +
                "\n" +
                "/**\n" +
                " * 创建并发送一封包含文本、图片、附件的复杂邮件\n" +
                " * \n" +
                " * JavaMail 版本: 1.6.0\n" +
                " * JDK 版本: JDK 1.7 以上（必须）\n" +
                " * \n" +
                " * @author xietansheng\n" +
                " */\n" +
                "public class Main {\n" +
                "\n" +
                "    // 发件人的 邮箱 和 密码（替换为自己的邮箱和密码）\n" +
                "    public static String myEmailAccount = \"xxxxxxxx@163.com\";\n" +
                "    public static String myEmailPassword = \"xxxxxxxx\";\n" +
                "\n" +
                "    // 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般格式为: smtp.xxx.com\n" +
                "    // 网易163邮箱的 SMTP 服务器地址为: smtp.163.com\n" +
                "    public static String myEmailSMTPHost = \"smtp.163.com\";\n" +
                "\n" +
                "    // 收件人邮箱（替换为自己知道的有效邮箱）\n" +
                "    public static String receiveMailAccount = \"xxxxxxxxx@qq.com\";\n" +
                "\n" +
                "    public static void main(String[] args) throws Exception {\n" +
                "        // 1. 创建参数配置, 用于连接邮件服务器的参数配置\n" +
                "        Properties props = new Properties();                    // 参数配置\n" +
                "        props.setProperty(\"mail.transport.protocol\", \"smtp\");   // 使用的协议（JavaMail规范要求）\n" +
                "        props.setProperty(\"mail.smtp.host\", myEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址\n" +
                "        props.setProperty(\"mail.smtp.auth\", \"true\");            // 需要请求认证\n" +
                "        \n" +
                "        // 开启 SSL 连接, 以及更详细的发送步骤请看上一篇: 基于 JavaMail 的 Java 邮件发送：简单邮件发送\n" +
                "\n" +
                "        // 2. 根据配置创建会话对象, 用于和邮件服务器交互\n" +
                "        Session session = Session.getInstance(props);\n" +
                "        session.setDebug(true);                                 // 设置为debug模式, 可以查看详细的发送 log\n" +
                "\n" +
                "        // 3. 创建一封邮件\n" +
                "        MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);\n" +
                "\n" +
                "        // 也可以保持到本地查看\n" +
                "        // message.writeTo(file_out_put_stream);\n" +
                "\n" +
                "        // 4. 根据 Session 获取邮件传输对象\n" +
                "        Transport transport = session.getTransport();\n" +
                "\n" +
                "        // 5. 使用 邮箱账号 和 密码 连接邮件服务器\n" +
                "        //    这里认证的邮箱必须与 message 中的发件人邮箱一致，否则报错\n" +
                "        transport.connect(myEmailAccount, myEmailPassword);\n" +
                "\n" +
                "        // 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人\n" +
                "        transport.sendMessage(message, message.getAllRecipients());\n" +
                "\n" +
                "        // 7. 关闭连接\n" +
                "        transport.close();\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 创建一封复杂邮件（文本+图片+附件）\n" +
                "     */\n" +
                "    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {\n" +
                "        // 1. 创建邮件对象\n" +
                "        MimeMessage message = new MimeMessage(session);\n" +
                "\n" +
                "        // 2. From: 发件人\n" +
                "        message.setFrom(new InternetAddress(sendMail, \"我的测试邮件_发件人昵称\", \"UTF-8\"));\n" +
                "\n" +
                "        // 3. To: 收件人（可以增加多个收件人、抄送、密送）\n" +
                "        message.addRecipient(RecipientType.TO, new InternetAddress(receiveMail, \"我的测试邮件_收件人昵称\", \"UTF-8\"));\n" +
                "\n" +
                "        // 4. Subject: 邮件主题\n" +
                "        message.setSubject(\"TEST邮件主题（文本+图片+附件）\", \"UTF-8\");\n" +
                "\n" +
                "        /*\n" +
                "         * 下面是邮件内容的创建:\n" +
                "         */\n" +
                "\n" +
                "        // 5. 创建图片“节点”\n" +
                "        MimeBodyPart image = new MimeBodyPart();\n" +
                "        DataHandler dh = new DataHandler(new FileDataSource(\"FairyTail.jpg\")); // 读取本地文件\n" +
                "        image.setDataHandler(dh);\t\t            // 将图片数据添加到“节点”\n" +
                "        image.setContentID(\"image_fairy_tail\");\t    // 为“节点”设置一个唯一编号（在文本“节点”将引用该ID）\n" +
                "\n" +
                "        // 6. 创建文本“节点”\n" +
                "        MimeBodyPart text = new MimeBodyPart();\n" +
                "        //    这里添加图片的方式是将整个图片包含到邮件内容中, 实际上也可以以 http 链接的形式添加网络图片\n" +
                "        text.setContent(\"这是一张图片<br/><img src='cid:image_fairy_tail'/>\", \"text/html;charset=UTF-8\");\n" +
                "\n" +
                "        // 7. （文本+图片）设置 文本 和 图片 “节点”的关系（将 文本 和 图片 “节点”合成一个混合“节点”）\n" +
                "        MimeMultipart mm_text_image = new MimeMultipart();\n" +
                "        mm_text_image.addBodyPart(text);\n" +
                "        mm_text_image.addBodyPart(image);\n" +
                "        mm_text_image.setSubType(\"related\");\t// 关联关系\n" +
                "\n" +
                "        // 8. 将 文本+图片 的混合“节点”封装成一个普通“节点”\n" +
                "        //    最终添加到邮件的 Content 是由多个 BodyPart 组成的 Multipart, 所以我们需要的是 BodyPart,\n" +
                "        //    上面的 mm_text_image 并非 BodyPart, 所有要把 mm_text_image 封装成一个 BodyPart\n" +
                "        MimeBodyPart text_image = new MimeBodyPart();\n" +
                "        text_image.setContent(mm_text_image);\n" +
                "\n" +
                "        // 9. 创建附件“节点”\n" +
                "        MimeBodyPart attachment = new MimeBodyPart();\n" +
                "        DataHandler dh2 = new DataHandler(new FileDataSource(\"妖精的尾巴目录.doc\"));  // 读取本地文件\n" +
                "        attachment.setDataHandler(dh2);\t\t\t                                    // 将附件数据添加到“节点”\n" +
                "        attachment.setFileName(MimeUtility.encodeText(dh2.getName()));\t            // 设置附件的文件名（需要编码）\n" +
                "\n" +
                "        // 10. 设置（文本+图片）和 附件 的关系（合成一个大的混合“节点” / Multipart ）\n" +
                "        MimeMultipart mm = new MimeMultipart();\n" +
                "        mm.addBodyPart(text_image);\n" +
                "        mm.addBodyPart(attachment);\t\t// 如果有多个附件，可以创建多个多次添加\n" +
                "        mm.setSubType(\"mixed\");\t\t\t// 混合关系\n" +
                "\n" +
                "        // 11. 设置整个邮件的关系（将最终的混合“节点”作为邮件的内容添加到邮件对象）\n" +
                "        message.setContent(mm);\n" +
                "\n" +
                "        // 12. 设置发件时间\n" +
                "        message.setSentDate(new Date());\n" +
                "\n" +
                "        // 13. 保存上面的所有设置\n" +
                "        message.saveChanges();\n" +
                "\n" +
                "        return message;\n" +
                "    }\n" +
                "\n" +
                "}\n";
        String c2="1. 我使⽤的是text的，因为blob存储的是⼆进制格式，所以长⽂本⾥⾯的搜有数据都是⼆进制数据，在上传图⽂的时候，传过来是String,\n" +
                "保存起来，读取都需要转换，还有javaBean设计的时候也要把⽂本的类型弄成BLOB类型，在加上hibernate 4 ，⽣成blob对象还要\n" +
                "LobHelper,各种⿇烦，于是就使⽤text来存储。\n" +
                "2 . 那么如何存储⽂章中的图⽚呢，我的处理是把⽂字与图⽚的地址都存储成text类型，使⽤特定的标签把图⽚地址包起来，\n" +
                "然后在解析的时候，使⽤正则表达式把图⽚拿出来，在去服务器去访问。\n" +
                "3. 当你把⽂章存进去之后，会发现使⽤⽂章中包含换⾏符之后的⽂字都不见了，其实他⾥⾯的数据还是有的，为了⽅便我\n" +
                "把换⾏符都换为了\n" +
                "--------------------------------------------------------\n" +
                "作者：八卦鹏海13\n" +
                "链接：https://wenku.baidu.com/view/6eecbd395aeef8c75fbfc77da26925c52cc591e4.html\n" +
                "来源：百度文库\n" +
                "著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。";
        int length = c1.getBytes(StandardCharsets.UTF_8).length;
        int length2 = c2.getBytes(StandardCharsets.UTF_8).length;
        log.info(length+"==========="+length2);

        User2 build = User2.builder().name("build1")
                .age(13)
                .content(c1)
                .build();
        User2 build2 = User2.builder().name("build2")
                .age(20)
                .content(c2)
                .build();
        List<User2> list=new ArrayList<>();
        list.add(build2);
        list.add(build);
        boolean save = userService2.saveBatch(list);
        log.info(JSON.toJSONString(build2));
    }

    private void method06() {

        LambdaQueryWrapper<User2> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(User2::getId,"000000000000");
        List<User2> list = userService2.list(wrapper);
        list.size();
    }

    private void method04() {
        String a="";
        User liu = User.builder()
                .id("1463072998929006594")
                .name(a)
                .age("550")
                .build();
        LambdaUpdateWrapper<User> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(User::getName,liu.getName());
        wrapper.set(User::getName,a);
//        userService.update(wrapper);
        userService.updateById(liu);
    }

    private void method03() {
        User liu = User.builder()
                .id("1463072998929006594")
                .name("aaaa")
                .age("232")
                .build();
        User lei = User.builder()
                .id("1457023928059985922")
                .name("bbb")
                .age("2323")
                .build();
        List<User> list=new ArrayList<>();
        list.add(lei);
        list.add(liu);
        List<User> collect = list.stream().map(user -> {
            User user1 = User.builder()
                    .id(user.getId())
                    .age(user.getAge())
                    .build();
            return user1;
        }).collect(Collectors.toList());
        LambdaUpdateWrapper<User> wrapper=new LambdaUpdateWrapper<>();
        userService.updateBatchById(collect);
    }

    private void method02() {
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        wrapper.select(User::getId,User::getAge);
        wrapper.eq(User::getName,"lei");
        wrapper.notIn(User::getAge,"0","98");
        List<User> list = userService.list(wrapper);
        log.info(JSON.toJSONString(list));
    }

    private void method01() throws InterruptedException {
        User liu = User.builder()
                .name("lei")
                .age("24")
                .build();
        userService.save(liu);
        log.info("insert success");

        asnyHelper.testASNY();
        LambdaUpdateWrapper<User> wrapper=new LambdaUpdateWrapper<>();
        wrapper.setSql("ID=(CASE NAME WHEN 'liu' THEN '77' ELSE ID END )");
        wrapper.setSql("AGE=(CASE NAME WHEN 'liu' THEN AGE ELSE '99' END )");
        userService.update(wrapper);
        log.info("结束");
//        List<TableVO> maps = commonMapper.queryTableNameList();
//        List<TableDDLVO> strings = commonMapper.getDDL("T_USER");
//        List<TableInfoVO> t_user = commonMapper.getTableInfo("T_USER");
//        List<IdxInfoVO> t_user1 = commonMapper.getIdxInfo("T_USER");
    }

}
